<template>
  <div class="targetSettingsPage">
    <el-card class="card-box" shadow="never">
      <div class="title-box d-flex a-center j-center">
          <CommonTitle titleName="资金预算绩效设置" style="margin-bottom: 0;"/>
      </div>
      <el-divider />
      <div class="content-box d-flex a-center j-center">
          <el-form
                  ref="ruleFormRef"
                  :model="form"
                  :rules="rules"
                  label-width="150px"
                  style="width: 100%;"
          >
              <el-row class="d-flex">
                  <el-col :span="11">
                      <el-form-item label="预算目标" prop="budgetTarget">
                          <el-input
                                  v-model="form.budgetTarget"
                                  placeholder="请输入预算目标"
                                  @input="form.budgetTarget = limitInputNum(form.budgetTarget,3,0,false)"
                          ></el-input>
                      </el-form-item>
                  </el-col>
                  <el-col :span="2"></el-col>
                  <el-col :span="11">
                      <el-form-item label="是否开启预算目标" prop="status">
                          <el-switch v-model="form.status" active-color="#fe8740" :active-value="1" :inactive-value="-1"/>
                      </el-form-item>
                  </el-col>
              </el-row>
              <el-row class="d-flex">
                  <el-col :span="11">
                      <el-form-item label="计划内占用" prop="insidePlanRate">
                          <el-input
                                  v-model="form.insidePlanRate"
                                  placeholder="请输入计划内占用"
                                  @input="form.insidePlanRate = limitInputNum(form.insidePlanRate,3,0,false)"
                          ></el-input>
                      </el-form-item>
                  </el-col>
                  <el-col :span="2"></el-col>
                  <el-col :span="11">
                      <el-form-item label="计划外占用" prop="outsidePlanRate">
                          <el-input
                                  v-model="form.outsidePlanRate"
                                  placeholder="请输入计划外占用"
                                  @input="form.outsidePlanRate = limitInputNum(form.outsidePlanRate,3,0,false)"
                          ></el-input>
                      </el-form-item>
                  </el-col>
              </el-row>
          </el-form>
      </div>
      <div class="buttons-box d-flex j-center a-center">
          <el-button
                  :loading="loading"
                  color="#FC7018"
                  style="color: #FFFFFF;"
                  @click="onSave">保存</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import {ElCard, ElDivider, ElForm, ElFormItem, ElRow, ElCol, ElSwitch, ElMessage} from "element-plus";
import CommonTitle from "@/components/CommonTitle.vue";
import {onMounted, reactive, ref} from "vue";
import {fundManagementApi} from "@/api/businessFinance";
import {limitInputNum} from "@/common/utils/GlobalUtils";
import router from "@/router";

const form = reactive({
    ruleId:'',
    budgetTarget:'',
    status:1,
    insidePlanRate:'',
    outsidePlanRate:''
});
const rules = reactive({
    budgetTarget:[
        { required: true, message: '预算目标不能为空!', trigger: 'blur' },
    ],
    insidePlanRate:[
        { required: true, message: '计划内占比不能为空!', trigger: 'blur' },
    ],
    outsidePlanRate:[
        { required: true, message: '计划外占比不能为空!', trigger: 'blur' },
    ],
});

const getFundBudgetExamineRuleVo = () => {
  fundManagementApi.getFundBudgetExamineRuleVo().then(res=>{
    if(res.code === "200"){
        for(let key in form){
            for (let k in res.data){
                if(key === k){
                    form[key] = res.data[k];
                }
            }
        }
    }
  });
}

const loading = ref(false);
const ruleFormRef = ref(null);
const onSave = () => {
    ruleFormRef.value.validate((valid, fields) => {
        if (valid) {
            loading.value = true;
            fundManagementApi.saveFundBudgetExamineRule(form).then(res=>{
                if(res.code === '200'){
                    router.push({path:`/businessFinance/fundManagement/fundBudgetManagement/performanceQuery`});
                    loading.value = false;
                    ElMessage.success('操作成功!')
                }
            });
        } else {
            console.log('error submit!', fields)
        }
    })
}

onMounted(()=>{
    getFundBudgetExamineRuleVo();
});
</script>

<style lang="scss" scoped>

</style>
